// pages/shop/shop.js
import {
  showModal
} from "../../utils/asyncVX"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    totalPrice:0,
    totalNum:0,
    allChecked:false
  },
  add() {
    wx.getSetting({
      success(res) {
        console.log(res)
        if (res.authSetting['scope.address'] == true) {
          wx.chooseAddress({
            success(address) {
              console.log('收货地址', address)
              //给地址对象address增加属性:拼接其它，存储整个收货地址信息
              address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
              wx.setStorageSync('address', address)
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
      onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let cart = wx.getStorageSync('cart') || []
    this.setData({
      cart
    })
    let address = wx.getStorageSync('address', address)
    this.setData({
      address: address
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  //提交
 async onClickButton(){
      const {address,totalNum}=this.data
      if(!address.userName){
        await showModal({content:"你还没有选择收货地址"})
        return
      }

      //判读商品是否选中
      if(totalNum==0){
        await showModal({content:"请选择中商品"})
        return  
      }
      wx.navigateTo({
        url: '../pay/index',
      })
  },

  //增加
  addd(e){
    console.log(e);
    let index=e.currentTarget.dataset.index
    let {cart}=this.data
    cart[index].num++
    this.ShopCart(cart)
  },
 async jian(e){
    let index=e.currentTarget.dataset.index
    let {cart}=this.data
    cart[index].num--
    if(cart[index].num<1){
      const res=await showModal({content:"你确定要删除?"})
      console.log(1);
      if(res.confirm){
        cart.splice(index,1)
      }else{
        return
      }
    }
    this.ShopCart(cart)
  },
  //反选
  handeItemChange(e){
    // console.log(e);
    let index=e.currentTarget.dataset.index
    let {cart}=this.data
    cart[index].checked!=cart[index].checked
    console.log(index,75)
    this.ShopCart(cart)
  },
  //全选
  allxuan(e){
    console.log(e);
    let {cart,allChecked}=this.data
    allChecked=!allChecked
    cart.forEach(item=>{
        item.checked=allChecked
    }) 
    this.ShopCart(cart)
  },
  //底部合计数量统计
  ShopCart(cart){
    let totalPrice=0,totalNum=0,allChecked=true
    cart.forEach(item=>{
      if(item.checked==true){
        totalPrice+=item.goods_price*item.num
        totalNum+=item.num
      }else{
        allChecked=false
      }
    })
    if(cart.length==0){
      allChecked=false
    }
    this.setData({
      cart,totalPrice,totalNum,allChecked
    })
    wx.setStorageSync('cart', cart)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})